package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入购物车的商品是否已经存在了
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在了，只需要将数量加一
        if (list != null && list.size() > 0) {
            for (ShoppingCart cart : list) {
                cart.setNumber(cart.getNumber() + 1);
//                todomapper更新菜品
                shoppingCartMapper.update(cart);
            }
            return;
        }

        //如果不存在，需插入一条购物车数据
        if (shoppingCart.getDishId() != null) {
            //添加进来的是菜品
            Dish dish = dishMapper.getById(shoppingCart.getDishId().toString());
            BeanUtils.copyProperties(dish, shoppingCart, "id");
            shoppingCart.setAmount(dish.getPrice());
        } else {
            //添加进来的是套餐
            SetmealVO setmealVO = setmealMapper.queryById(shoppingCart.getSetmealId());
            BeanUtils.copyProperties(setmealVO, shoppingCart, "id");
            shoppingCart.setAmount(setmealVO.getPrice());
        }

        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());

        shoppingCartMapper.insert(shoppingCart);


    }

    /**
     * 查看购物车方法
     *
     * @return
     */
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断购物车当前商品加购数量是否大于2
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = shoppingCarts.get(0);
        if (cart.getNumber() > 1) {
            //大于1当前商品数量减一
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapper.update(cart);

        } else {
            //当前商品加购数量小于2直接删除当前商品
            shoppingCartMapper.deleteById(cart);
        }


    }
}
